import * as types from './types'

import getCnode from './datas/getCnode'

//页数
const limit = 10;
const URL = "https://cnodejs.org/api/v1/";

/**获取文章列表 *
**tab 文章类型 *
**page 页码 */
export const getNodeList = ({ commit },data) => {
    console.log("我在action page="+data)
    let str = URL + "topics?mdrender='true'" + "&&tab=" + data.tab + "&&page=" + data.page + "&&limit=" + limit
    let callBack = null;
    if(data.page == 1){
        callBack = fistGetNodeList;
    }else{
        callBack = getNodeListMore;
    }

    commit(types.PAGE_SET, {
        data: data.page
    })

    getCnode(commit , str , callBack);

}
/**获取文章数据后的回调函数 */
/**首次拉取文章 */
const fistGetNodeList = ( commit , data ) => { 
    commit(types.NODELIST_RESET, {
        data: data.data
    })
}
/**加载更多文章 */
const getNodeListMore = ( commit , data ) => { 
    commit(types.NODELIST_ADD, {
        data: data.data
    })
}


/**获取文章内容 */
export const getNode = ({ commit },id) => {
    let str = URL + "topic/" + id;
    //清除之前的文章内容
    commit(types.NODE_RESET, {
        data: null
    })
    //获取新内容
    getCnode(commit , str , getNodeBack);
}
const getNodeBack = ( commit , data ) => { 
    commit(types.NODE_RESET, {
        data: data.data.content
    })
}